package cn.com.css.taxInspection.workflow.controller;

import cn.com.css.taxInspection.common.aop.annotation.LogAnnotation;
import cn.com.css.taxInspection.workflow.entity.WorkflowLogEntity;
import cn.com.css.taxInspection.workflow.mapper.WorkflowNotificationMapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.gt3.entity.DmGySwjgEntity;
import cn.com.css.taxInspection.gt3.entity.JcAjxxEntity;
import cn.com.css.taxInspection.gt3.service.DmGySwjgService;
import cn.com.css.taxInspection.gt3.service.JcAjxxService;
import cn.com.css.taxInspection.system.entity.SysUser;
import cn.com.css.taxInspection.system.service.UserService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.workflow.entity.WorkflowNotificationEntity;
import cn.com.css.taxInspection.workflow.service.WorkflowNotificationService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

@Controller
@RequestMapping("/workflow/notification/")
public class WorkflowNotificationController {
    @Resource
    private WorkflowNotificationService workflowNotificationService;
    @Resource
    private JcAjxxService jcAjxxService;
    @Resource
    private HttpSessionService httpSessionService;
    @Resource
    private DmGySwjgService dmGySwjgService;
    @Resource
    private UserService userService;

    @Resource
    private WorkflowNotificationMapper workflowNotificationMapper;

    @ApiOperation(value = "填写保存")
    @PostMapping("/write")
    @RequiresPermissions("workflow:notification:write")
    @ResponseBody
    public DataResult add(@RequestBody WorkflowNotificationEntity workflowNotification) {
        String currentDeptId = httpSessionService.getCurrentDeptId();
        String currentUserId = httpSessionService.getCurrentUserId();
        String relevance = workflowNotification.getRelevance();
        if (TsBqUtils.isNull(relevance)) {
            return DataResult.fail("Relevance不能为NULL！");
        }
        String notificationFile = workflowNotification.getNotificationFile();
        if (TsBqUtils.isNull(notificationFile)) {
            return DataResult.fail("notificationFile不能为NULL！");
        }
        String notificationNo = workflowNotification.getNotificationNo();
        if (TsBqUtils.isNull(notificationNo)) {
            return DataResult.fail("notificationNo不能为NULL！");
        }
        WorkflowNotificationEntity notificationEntity = new WorkflowNotificationEntity();
        notificationEntity.setId(TsBqUtils.getUUID());
        notificationEntity.setRelevance(relevance);
        notificationEntity.setNotificationNo(notificationNo);
        notificationEntity.setNotificationFile(notificationFile);
        notificationEntity.setTaxorgcode(currentDeptId);
        notificationEntity.setDeleted(Constants.DATA_NOT_DELETED);
        notificationEntity.setCreateTime(new Date());
        notificationEntity.setCreateId(currentUserId);
        workflowNotificationService.save(notificationEntity);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("/list0")
    @RequiresPermissions("workflow:notification:list")
    @ResponseBody
    public DataResult list0(@RequestBody WorkflowNotificationEntity workflowNotification) throws ParseException {
        List<JcAjxxEntity> iPage = jcAjxxService.pageInfoByNotification0(workflowNotification);
        return DataResult.success(iPage);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @ApiOperation(value = "查询分页数据")
    @PostMapping("/list1")
    @RequiresPermissions("workflow:notification:list")
    @ResponseBody
    public DataResult list1(@RequestBody WorkflowNotificationEntity workflowNotification) {
        String currentUserId = httpSessionService.getCurrentUserId();
        Page page = new Page(workflowNotification.getPage(), workflowNotification.getLimit());
        LambdaQueryWrapper<WorkflowNotificationEntity> queryWrapper = Wrappers.lambdaQuery();
        // 查询条件示例
        queryWrapper.eq(WorkflowNotificationEntity::getCreateId, currentUserId);
        IPage<WorkflowNotificationEntity> iPage = workflowNotificationService.page(page, queryWrapper);
        List<WorkflowNotificationEntity> records = iPage.getRecords();
        for (WorkflowNotificationEntity notification : records) {
            String relevance = notification.getRelevance();
            String notificationFile = notification.getNotificationFile();
            String notificationNo = notification.getNotificationNo();
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, notification);
            notification.setNotificationFile(notificationFile);
            notification.setNotificationNo(notificationNo);
        }
        return DataResult.success(iPage);
    }


    @SuppressWarnings({"rawtypes", "unchecked", "null"})
    @ApiOperation(value = "查询分页数据")
    @PostMapping("/list")
    @RequiresPermissions("workflow:notification:list")
    @ResponseBody
    public DataResult list(@RequestBody WorkflowNotificationEntity workflowNotification) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDeptId = httpSessionService.getCurrentDeptId();
        DmGySwjgEntity dmGySwjgEntity = dmGySwjgService.getSwjgDm(currentDeptId);
        String sjswjgDm = dmGySwjgEntity.getSjswjgDm();
        Page page = new Page(workflowNotification.getPage(), workflowNotification.getLimit());
        LambdaQueryWrapper<WorkflowNotificationEntity> queryWrapper = Wrappers.lambdaQuery();
        //如果BZ标志是空的，全查询全局，否则查询全部
        if (TsBqUtils.isNull(workflowNotification.getBz())) {
            queryWrapper.like(WorkflowNotificationEntity::getTaxorgcode, sjswjgDm.substring(0, 9));
        }
        // 如果科室列表不是空，则全查
        if (TsBqUtils.isNotNull(workflowNotification.getJcbmDmList())) {
            queryWrapper.in(WorkflowNotificationEntity::getTaxorgcode, workflowNotification.getJcbmDmList());
        }
        if (!StringUtils.isEmpty(workflowNotification.getStartTime())) {
            queryWrapper.gt(WorkflowNotificationEntity::getCreateTime, sdf.parse(workflowNotification.getStartTime()));
        }
        if (!StringUtils.isEmpty(workflowNotification.getEndTime())) {
            queryWrapper.lt(WorkflowNotificationEntity::getCreateTime, sdf.parse(workflowNotification.getEndTime()));
        }
        boolean flag = false;
        JcAjxxEntity jcAjxx = new JcAjxxEntity();
        //案件编号
        if (!StringUtils.isEmpty(workflowNotification.getAjbh())) {
            jcAjxx.setJcajbh(workflowNotification.getAjbh());
            flag = true;
        }
        //纳税人识别号
        if (!StringUtils.isEmpty(workflowNotification.getNsrsbh())) {
            jcAjxx.setNsrsbh(workflowNotification.getNsrsbh());
            flag = true;
        }
        //纳税人名称
        if (!StringUtils.isEmpty(workflowNotification.getNsrmc())) {
            jcAjxx.setNsrmc(workflowNotification.getNsrmc());
            flag = true;
        }

        //如果搜索这三个条件，就取案件里查，jcajxxuuid，因为表里没有纳税人信息字段
        if (flag) {
            List<JcAjxxEntity> list = jcAjxxService.getByJcAjxx(jcAjxx);
            if (TsBqUtils.isNotNull(list)) {
                List<String> listJcuuid = new ArrayList<>();
                for (JcAjxxEntity jcAjxxEntity : list) {
                    String jcajxxuuid = jcAjxxEntity.getJcajxxuuid();
                    listJcuuid.add(jcajxxuuid);
                }
                queryWrapper.in(WorkflowNotificationEntity::getRelevance, listJcuuid);
            } else {
                return DataResult.fail("未查询到！");
            }
        }
        queryWrapper.orderByDesc(WorkflowNotificationEntity::getCreateTime);
        IPage<WorkflowNotificationEntity> iPage = workflowNotificationService.page(page, queryWrapper);
        List<WorkflowNotificationEntity> records = iPage.getRecords();
        for (WorkflowNotificationEntity notification : records) {
            String relevance = notification.getRelevance();
            String notificationFile = notification.getNotificationFile();
            String notificationNo = notification.getNotificationNo();
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, notification);
            notification.setNotificationFile(notificationFile);
            notification.setNotificationNo(notificationNo);
            notification.setField6("已送达");
            String createId = notification.getCreateId();
            SysUser sysUser = userService.getById(createId);
            if (TsBqUtils.isNotNull(sysUser)) {
                String nickName = sysUser.getNickName();
                notification.setCreateId(nickName);
            } else {
                notification.setCreateId(createId);
            }
            String jcbm_dm = notification.getJcbmDm();
            if (TsBqUtils.isNotNull(jcbm_dm)) {
                DmGySwjgEntity dmGySwjg = dmGySwjgService.getSwjgDm(jcbm_dm);
                if (TsBqUtils.isNotNull(dmGySwjg)) {
                    String swjgmc = dmGySwjg.getSwjgmc();
                    notification.setJcbmDm(swjgmc);
                }
            }
        }
        return DataResult.success(iPage);
    }


    @ApiOperation(value = "查询分页数据")
    @PostMapping("/listtj")
    @RequiresPermissions("workflow:notification:list")
    @ResponseBody
    public DataResult listtj(@RequestBody WorkflowNotificationEntity workflowNotification) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDeptId = httpSessionService.getCurrentDeptId();
        DmGySwjgEntity dmGySwjgEntity = dmGySwjgService.getSwjgDm(currentDeptId);
        String sjswjgDm = dmGySwjgEntity.getSjswjgDm();
        Page page = new Page(workflowNotification.getPage(), workflowNotification.getLimit());
        LambdaQueryWrapper<WorkflowNotificationEntity> queryWrapper = Wrappers.lambdaQuery();
        //如果BZ标志是空的，全查询全局，否则查询全部
        if (workflowNotification.getBz().equals("mh")) {
            queryWrapper.like(WorkflowNotificationEntity::getTaxorgcode,workflowNotification.getTaxorgcode());
        }else if (workflowNotification.getBz().equals("bmh")){
            queryWrapper.eq(WorkflowNotificationEntity::getTaxorgcode,workflowNotification.getTaxorgcode());
        }
        // 如果科室列表不是空，则全查
        if (TsBqUtils.isNotNull(workflowNotification.getJcbmDmList())) {
            queryWrapper.in(WorkflowNotificationEntity::getTaxorgcode, workflowNotification.getJcbmDmList());
        }
        if (!StringUtils.isEmpty(workflowNotification.getStartTime())) {
            queryWrapper.gt(WorkflowNotificationEntity::getCreateTime, sdf.parse(workflowNotification.getStartTime()));
        }
        if (!StringUtils.isEmpty(workflowNotification.getEndTime())) {
            queryWrapper.lt(WorkflowNotificationEntity::getCreateTime, sdf.parse(workflowNotification.getEndTime()));
        }
        boolean flag = false;
        JcAjxxEntity jcAjxx = new JcAjxxEntity();
        //案件编号
        if (!StringUtils.isEmpty(workflowNotification.getAjbh())) {
            jcAjxx.setJcajbh(workflowNotification.getAjbh());
            flag = true;
        }
        //纳税人识别号
        if (!StringUtils.isEmpty(workflowNotification.getNsrsbh())) {
            jcAjxx.setNsrsbh(workflowNotification.getNsrsbh());
            flag = true;
        }
        //纳税人名称
        if (!StringUtils.isEmpty(workflowNotification.getNsrmc())) {
            jcAjxx.setNsrmc(workflowNotification.getNsrmc());
            flag = true;
        }

        //如果搜索这三个条件，就取案件里查，jcajxxuuid，因为表里没有纳税人信息字段
        if (flag) {
            List<JcAjxxEntity> list = jcAjxxService.getByJcAjxx(jcAjxx);
            if (TsBqUtils.isNotNull(list)) {
                List<String> listJcuuid = new ArrayList<>();
                for (JcAjxxEntity jcAjxxEntity : list) {
                    String jcajxxuuid = jcAjxxEntity.getJcajxxuuid();
                    listJcuuid.add(jcajxxuuid);
                }
                queryWrapper.in(WorkflowNotificationEntity::getRelevance, listJcuuid);
            } else {
                return DataResult.fail("未查询到！");
            }
        }
        queryWrapper.orderByDesc(WorkflowNotificationEntity::getCreateTime);
        IPage<WorkflowNotificationEntity> iPage = workflowNotificationService.page(page, queryWrapper);
        List<WorkflowNotificationEntity> records = iPage.getRecords();
        for (WorkflowNotificationEntity notification : records) {
            String relevance = notification.getRelevance();
            String notificationFile = notification.getNotificationFile();
            String notificationNo = notification.getNotificationNo();
            JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(relevance);
            BeanUtils.copyProperties(jcAjxxEntity, notification);
            notification.setNotificationFile(notificationFile);
            notification.setNotificationNo(notificationNo);
            notification.setField6("已送达");
            String createId = notification.getCreateId();
            SysUser sysUser = userService.getById(createId);
            if (TsBqUtils.isNotNull(sysUser)) {
                String nickName = sysUser.getNickName();
                notification.setCreateId(nickName);
            } else {
                notification.setCreateId(createId);
            }
            String jcbm_dm = notification.getJcbmDm();
            if (TsBqUtils.isNotNull(jcbm_dm)) {
                DmGySwjgEntity dmGySwjg = dmGySwjgService.getSwjgDm(jcbm_dm);
                if (TsBqUtils.isNotNull(dmGySwjg)) {
                    String swjgmc = dmGySwjg.getSwjgmc();
                    notification.setJcbmDm(swjgmc);
                }
            }
        }
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "保存廉政告知书")
    @PostMapping("/saveLzgzs")
    @ResponseBody
    public DataResult saveLzgzs(@RequestBody WorkflowNotificationEntity workflowNotification) {
        return DataResult.success(workflowNotificationService.updateById(workflowNotification));
    }


    @ApiOperation(value = "廉政告知书统计(市局)")
    @LogAnnotation(title = "廉政告知书", action = "市级统计")
    @PostMapping("/LzgzsSjtj")
    @ResponseBody
    public DataResult LzgzsSjtj(@RequestBody WorkflowNotificationEntity workflowNotification) {
        String startTime = workflowNotification.getStartTime();
        String endTime = workflowNotification.getEndTime();
        List<WorkflowLogEntity> workflowLogEntityList = workflowNotificationMapper.querySjRztj(startTime, endTime);
        return DataResult.success(workflowLogEntityList);

    }


    @ApiOperation(value = "廉政告知书局长统计")
    @LogAnnotation(title = "廉政告知书局长统计", action = "局长统计")
    @PostMapping("/LzgzsJztj")
    @ResponseBody
    public DataResult LzgzsJztj(@RequestBody WorkflowLogEntity workflowLogEntity)  {
        String startTime = workflowLogEntity.getStartTime();
        String endTime = workflowLogEntity.getEndTime();
        String currentDeptId = httpSessionService.getCurrentDeptId();
        DmGySwjgEntity dmGySwjgEntity = dmGySwjgService.getSwjgDm(currentDeptId);
        String sjswjgDm = dmGySwjgEntity.getSjswjgDm();
        int bz = workflowLogEntity.getBz();
        String substringsjswjgDm = sjswjgDm.substring(0, bz);
        List<WorkflowLogEntity> workflowLogEntityList=workflowNotificationMapper.querySjJztj(startTime,endTime,substringsjswjgDm);
        for (WorkflowLogEntity workflowLogEntity1 : workflowLogEntityList) {
            String _taxorgcode = workflowLogEntity1.getTaxorgcode();
            DmGySwjgEntity _dmGySwjgEntity = dmGySwjgService.getSwjgDm(_taxorgcode);
            String swjgmc = _dmGySwjgEntity.getSwjgmc();
            workflowLogEntity1.setTaxorg(swjgmc);
        }
        return DataResult.success(workflowLogEntityList);
    }

}
